// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.chips;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class ZXSpectrum48 implements Chip {
  private final Z80 z80;
  private final ULA ula;
  
  private Memory rom;
  private Memory videoRam;
  private Memory upperRam;
  
  public ZXSpectrum48() {
    this.z80 = new Z80();
    this.ula = new ULA(z80);
    this.rom = new Memory(14);
    this.videoRam = new Memory(14);
    this.upperRam = new Memory(15);
  }
  
  public void update() {
    z80.update();

    if (z80.ADDRESS_ACTIVE) {
      rom.ADDRESS = z80.ADDRESS;
      videoRam.ADDRESS = z80.ADDRESS;
      upperRam.ADDRESS = upperRam.ADDRESS;
      
      if ((z80.ADDRESS & 0x8000) == 0) {
        if ((z80.ADDRESS & 0x4000) == 0) {
          // ROM
          rom.ADDRESS = z80.ADDRESS;
          rom.RD = z80.RD;
          rom.REQ = z80.MREQ;
        } else {
          // Video RAM
          videoRam.ADDRESS = z80.ADDRESS;
          videoRam.RD = z80.RD;
          videoRam.WR = z80.WR;
          videoRam.REQ = z80.MREQ;
        }
      } else {
        // Upper RAM
        
      }
      memory.ADDRESS = z80.ADDRESS;
      memory.RD = z80.RD;
      memory.WR = z80.WR;
      if (memory.RD) {
        z80.DATA = memory.DATA;
      } else {
        memory.DATA = z80.DATA;
      }
    }
    
    memory.updateEndPoints();
  }
}
